#include "gaia_common.h"
#include "gaia_db.h"

//DB_RESULT gaia_db_vselect(const char *sql);
// just test 
/*
int main(void)
{
  MYSQL_RES *result;
  MYSQL_ROW sqlrow;
  int res = 0;

  char *add_sql = "insert into gaia values(100,'ssdfdh','uplooking');";
  res = gaia_DBinsert(add_sql);
  sleep(5);
  char *del_sql = "delete from gaia where pass='uplooking'";
  gaia_DBdelete(del_sql); 

  if(res)
    fprintf(stderr,"insert fail!\n");
  
  char *sql = "select hostid from gaia where username='uplooking' "; 
  result = gaia_db_vselect(sql);

  if(NULL == result)
  {
    fprintf(stderr,"execute sql error!\n");
    return 1;
  }else
    fprintf(stderr,"execute sql success!\n");

  printf("%lu Rows\n",(unsigned long)mysql_num_rows(result));   
  int j = mysql_num_fields(result);
  int i = 0;
  // 取出结果集 
  while(sqlrow = mysql_fetch_row(result))
  {
    for(i = 0;i < j;i++)
     printf("%s\n",sqlrow[i]);
  }
 
  mysql_free_result(result);
  return 0;
}
*/


/**********************************
 * 函数名：gaia_db_vselect
 * 函数功能: 得到一个结果集 
 * 返回值：NULL 或者MYSQL_RES
 *********************************/
DB_RESULT gaia_db_vselect(const char *sql)
{
  MYSQL *mysql;
  DB_RESULT res;

 /* 连接mysql */
  gaia_connect_mysql(&mysql); 

 if(NULL == mysql)
  return NULL;  

  /* 检查mysql错误类型 */
  if(0 != mysql_query(mysql,sql))
  {
    fprintf(stderr,"query error!\n");
    switch(mysql_errno(mysql))
    {
      case ER_SERVER_SHUTDOWN:
      case ER_UNKNOWN_ERROR:
        fprintf(stderr,"MySQL Server is Down!\n");
        res = NULL;
        break;
      case ER_ACCESS_DENIED_ERROR: /* wrong user or password */
        fprintf(stderr,"user or password is wrong!\n");
        res = NULL;
        break;
      case ER_ILLEGAL_GRANT_FOR_TABLE: /* user without any privileges */
      case ER_TABLEACCESS_DENIED_ERROR: /* user without some privilege */
        fprintf(stderr,"have some privileges problem!\n");
        res = NULL;
        break;
       default:
        res = NULL;
        break;
    }
  }else
  {
    res = mysql_store_result(mysql);
  }
  
  mysql_close(mysql);
  return res;
}

/**************************************
 * 函数名：gaia_DBinsert 
 * 函数功能：插入数据 
 * 返回值：成功 0，失败 1
 *************************************/
int gaia_DBinsert(const char *sql)
{
  MYSQL *mysql;
  int res = 0;
  gaia_connect_mysql(&mysql);
  //if( (mysql = gaia_connect_mysql()) == NULL)
  if(NULL == mysql)
  {
    fprintf(stderr,"connect mysql error %d: %s\n",mysql_errno(mysql),mysql_error(mysql));
    return 1;
  }
  /* 执行数据库操作 */
  res = mysql_query(mysql,sql);
 
  if(res)
  {
    fprintf(stderr,"insert error %d: %s\n",mysql_errno(mysql),mysql_error(mysql));
    return 1;
  }/*else{
    printf("insert %lu rows\n",(unsigned long)mysql_affected_rows(mysql));
  }*/
    
  return 0;
}

/****************************
 * 函数名；gaia_DBselect_nextid
 * 函数功能：查询nextid
 * 函数返回值：成功 0,失败　1
 ****************************/
int gaia_DBselect_nextid(int flag,int *nextid)
{
  char *sql = NULL; 
  //根据flag来进行判断sql
  if(0 == flag)
    sql = "select nextid from gaia_ids where table_name='gaia_host_info'";
  else
    sql = "select nextid from gaia_ids where table_name='gaia_process_status'";

  if(NULL == sql)
  {
   printf("sql error!\n");
   exit(1);
  }
  int res = 0;
  int i,j;

  MYSQL_ROW sqlrow;
  MYSQL_RES *mysql_res;
  MYSQL *mysql; 
  /* 连接数据库 */
  gaia_connect_mysql(&mysql);
  //if( (mysql = gaia_connect_mysql()) == NULL)
  if(NULL == mysql)
  {
    fprintf(stderr,"connect mysql error on select hostid %d: %s\n",mysql_errno(mysql),mysql_error(mysql));
    return 1;
  } 
  /* 执行查询操作 */
  res = mysql_query(mysql,sql);

  if(res)
  {
    fprintf(stderr,"select error %d: %s\n",mysql_errno(mysql),mysql_error(mysql));
    return 1;
  }
  /* 取出结果集 */
  mysql_res = mysql_store_result(mysql);   
  
  j = mysql_num_fields(mysql_res);
  // 取出记录 
  while( (sqlrow = mysql_fetch_row(mysql_res)))
  {
    for(i = 0;i < j;i++)
    {
      *nextid = atoi(sqlrow[i]);//nextid只有一个
      break;
    }
    break;
     //printf("%s\n",sqlrow[i]);
  }
  
  mysql_free_result(mysql_res);
  
  mysql_close(mysql);
  return 0;
}

/**************************************
 * 函数名；gaia_DBselect_task_status
 * 函数功能：查询task status
 * 函数返回值：成功 0,失败　1
 **************************************/
int gaia_DBselect_task_status(int task_id,int *status)
{
  //char *sql = NULL; 
  char sql[128]; 
  char *tmp_sql = "select status from gaia_process_status where  task_id=%d";

  sprintf(sql,tmp_sql,task_id);
  printf("%s\n",sql);

  if(NULL == sql)
  {
   printf("sql error!\n");
   exit(1);
  }
  int res = 0;
  int i,j;

  MYSQL_ROW sqlrow;
  MYSQL_RES *mysql_res;
  MYSQL *mysql; 
  /* 连接数据库 */
  gaia_connect_mysql(&mysql);
  //if( (mysql = gaia_connect_mysql()) == NULL)
  if(NULL == mysql)
  {
    fprintf(stderr,"connect mysql error on select hostid %d: %s\n",mysql_errno(mysql),mysql_error(mysql));
    return 1;
  } 
  /* 执行查询操作 */
  res = mysql_query(mysql,sql);

  if(res)
  {
    fprintf(stderr,"select error %d: %s\n",mysql_errno(mysql),mysql_error(mysql));
    return 1;
  }
  /* 取出结果集 */
  mysql_res = mysql_store_result(mysql);   
  
  j = mysql_num_fields(mysql_res);
  // 取出记录 
  while( (sqlrow = mysql_fetch_row(mysql_res)))
  {
    for(i = 0;i < j;i++)
    {
      *status = atoi(sqlrow[i]);//status只有一个
      break;
    }
    break;
     //printf("%s\n",sqlrow[i]);
  }
  
  mysql_free_result(mysql_res);
  
  mysql_close(mysql);
  return 0;
}

/***************************
 * 函数名：gaia_DBupdate 
 * 函数功能：更新数据 
 * 返回值：成功 0，失败 1
 **************************/
int gaia_DBupdate(const char *sql)
{
  MYSQL *mysql;
  int res;
  gaia_connect_mysql(&mysql);
  //if( (mysql = gaia_connect_mysql()) == NULL)
  if(NULL == mysql)
  {
    fprintf(stderr,"connect mysql error on update db %d: %s\n",mysql_errno(mysql),mysql_error(mysql));
    return 1;
  }

  res = mysql_query(mysql,sql);

  if(res)
  {
    fprintf(stderr, "update error %d: %sn",mysql_errno(mysql),mysql_error(mysql));  
    return 1;
  }/*else
    printf("update %lu rows\n",(unsigned long)mysql_affected_rows(mysql));   
   */
  mysql_close(mysql);
  //printf("update sucess\n");
  return 0;
}

/*****************************
 * 函数名：gaia_DBdelete 
 * 函数功能：删除数据 
 * 函数参数：sql，执行操作的sql
 * 返回值：成功 0，失败 1
 ****************************/
int gaia_DBdelete(const char *sql)
{
  MYSQL *mysql;
  int res;
  gaia_connect_mysql(&mysql);
  //if( (mysql = gaia_connect_mysql()) == NULL )
  if(NULL == mysql)
  {
    fprintf(stderr,"connect mysql error on delete %d: %s\n",mysql_errno(mysql),mysql_error(mysql));
    return 1;
  }

  res = mysql_query(mysql,sql);
  if(res)
  {
    fprintf(stderr,"delete error %d: %s\n",mysql_errno(mysql),mysql_error(mysql));
    return 1;
  }else{
    printf("delete %lu rows\n",(unsigned long)mysql_affected_rows(mysql));
  }

  return 0;
}
